package com.woniu.custom.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.woniu.common.utils.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import com.woniu.common.utils.JWTUtil;
import com.woniu.common.utils.ResponseData;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.custom.pojo.Addr;
import com.woniu.custom.mapper.AddrMapper;
import com.woniu.custom.service.AddrService;
@Service
public class AddrServiceImpl extends ServiceImpl<AddrMapper, Addr> implements AddrService{
    @Autowired
    private AddrMapper addrMapper;
    @Override
    public Addr selectAddr(String addressId) {
        return addrMapper.selectAddr(addressId);
    }

    @SneakyThrows
    @Override
    public ResponseData<?> addAddr(Addr newAddr) {
        Cookie[] cookies = request.getCookies();
        String jwt = null;
        for (Cookie cookie : cookies) {
            if("jwt".equals(cookie.getName())){
                jwt = cookie.getValue();
            }
        }
        Map<String, Object> userInfo = JWTUtil.getUserInfo(jwt);
        String userId = userInfo.get("id").toString();
        Addr addr = new Addr();
        long id = IdUtil.getSnowflake().nextId();
        addr.setId(String.valueOf(id));
        addr.setUserId(userId);
        addr.setDetail(newAddr.getDetail());
        addr.setCreateTime(new Date());
//        addr.setProvinceId();
        addr.setDefaultAddress(newAddr.getDefaultAddress());
        addr.setStatus(1);
        addr.setAddressTel(newAddr.getAddressTel());
        addr.setAddressName(newAddr.getAddressName());
        addr.setProvince(newAddr.getProvince());
        addr.setCity(newAddr.getCity());
        addr.setCounty(newAddr.getCounty());
        double[] doubles = MapUtil.addressToGPS(newAddr.getProvince() + newAddr.getCity() + newAddr.getCounty());
        addr.setLatitude(new BigDecimal(doubles[1]));
        addr.setLongitude(new BigDecimal(doubles[0]));

        Addr one = lambdaQuery().eq(Addr::getUserId, userId).eq(Addr::getDefaultAddress, 1).one();
        if(!ObjectUtil.isEmpty(one)){
            one.setDefaultAddress(0);
            updateById(one);
        }

        save(addr);
        return new ResponseData<>().success();
    }

    @Autowired
    private HttpServletRequest request;
    @SneakyThrows
    @Override
    public ResponseData<?> queryAddrByUserId() {
        Cookie[] cookies = request.getCookies();
        String jwt = null;
        for (Cookie cookie : cookies) {
            if("jwt".equals(cookie.getName())){
                jwt = cookie.getValue();
            }
        }
        Map<String, Object> userInfo = JWTUtil.getUserInfo(jwt);
        String userId = userInfo.get("id").toString();
        List<Addr> list = lambdaQuery().eq(Addr::getUserId, userId).list();
        return new ResponseData<>().success(list);
    }
}
